package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.dto.RoleMenuDto;
import net.zigin.nhi.could.waste.dto.RoleUserDto;
import net.zigin.nhi.could.waste.dto.SysRoleDto;
import net.zigin.nhi.could.waste.model.SysRole;
import net.zigin.nhi.could.waste.service.SysRoleService;

import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.SysRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SysRole控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysRoleController
 * @date 2021-9-7 19:08:55
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询角色详情
     *
     * @param id 角色ID
     * @return 角色详情
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("查询角色详情")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String")
    public JsonForResult<SysRoleVo> getById(@PathVariable String id) {
        AssertUtil.isNull(id, "角色id不能为空");
        SysRole role = sysRoleService.selectByPrimaryKey(id);
        SysRoleVo roleVo = ReflectUtil.copyProperties(role, SysRoleVo.class);
        return JsonForResult.<SysRoleVo>builder().data(roleVo).success(true).build();
    }

    /**
     * 保存或编辑角色
     *
     * @param roleDto 角色详情
     * @return 角色ID
     */
    @PostMapping("/save")
    @ApiOperation("保存或编辑角色")
    @ApiImplicitParam(name = "roleDto", value = "角色信息", required = true, dataType = "SysRoleDto")
    public JsonForResult save(@RequestBody SysRoleDto roleDto) {
        String id = sysRoleService.saveOrUpdate(roleDto);
        return JsonForResult.builder().data(id).success(true).build();
    }

    /**
     * 角色列表
     *
     * @param roleDto 角色查询条件
     * @return 角色列表
     */
    @PostMapping("/getList")
    @ApiOperation("角色列表")
    @ApiImplicitParam(name = "roleDto", value = "角色信息", required = true, dataType = "SysRoleDto")
    public JsonForResult<List<SysRoleVo>> getList(@RequestBody SysRoleDto roleDto) {
        List<SysRoleVo> roleList = sysRoleService.getRoleList(roleDto);
        Integer count = sysRoleService.getRoleCount(roleDto);
        return JsonForResult.<List<SysRoleVo>>builder()
                .data(roleList).total(count)
                .success(true).build();
    }

    /**
     * 获得用户关联的角色列表信息
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @GetMapping("/getListByUserId/{userId}")
    @ApiOperation("获得用户关联的角色列表信息")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    public JsonForResult<List<SysRoleVo>> getListByUserId(@PathVariable String userId) {
        List<SysRoleVo> roleVos = sysRoleService.infoByUserId(userId);
        return JsonForResult.<List<SysRoleVo>>builder().data(roleVos).success(true).build();
    }

    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        AssertUtil.isNull(id, "角色id不能为空");
        if (sysRoleService.judgeRemove(id)) {
            sysRoleService.remove(id);
            return JsonForResult.builder().success(true).build();
        } else {
            return JsonForResult.builder().data("该角色下存在用户！").success(false).build();
        }
    }

    /**
     * 角色菜单绑定
     *
     * @param roleMenuDto 角色绑定菜单参数
     * @return
     */
    @PostMapping("/concatMenu")
    @ApiOperation("角色菜单绑定")
    @ApiImplicitParam(name = "roleMenuDto", value = "角色绑定菜单信息", required = true, dataType = "RoleMenuDto")
    public JsonForResult concatMenu(@RequestBody RoleMenuDto roleMenuDto) {
        AssertUtil.isNull(roleMenuDto.getRoleId(), "角色id不能为空");
        AssertUtil.isNull(roleMenuDto.getMenuIds(), "菜单列表不能为空");
        sysRoleService.concatMenu(roleMenuDto.getRoleId(), roleMenuDto.getMenuIds());
        return JsonForResult.builder().success(true).build();
    }

    /**
     * 角色用户绑定
     *
     * @param roleUserDto 角色绑定用户信息
     * @return
     */
    @PostMapping("/concatUser")
    @ApiOperation("角色用户绑定")
    @ApiImplicitParam(name = "roleUserDto", value = "角色绑定用户信息", required = true, dataType = "RoleUserDto")
    public JsonForResult concatUser(@RequestBody RoleUserDto roleUserDto) {
        AssertUtil.isNull(roleUserDto.getUserId(), "用户id不能为空");
        AssertUtil.isNull(roleUserDto.getRoleIds(), "角色列表不能为空");
        sysRoleService.concatUser(roleUserDto.getRoleIds(), roleUserDto.getUserId());
        return JsonForResult.builder().success(true).build();
    }


}
